package com.kelvin.paopao.utils;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.kelvin.kelvinlib.utils.LogUtil;

/**
 * Created by MCK on 2017/5/11.
 */

public class AnimateHelper {
    /**
     * 开打dialog 旋转floatactionbutton
     */
    public static void fabOpen(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", -155, -135);
        animator.setDuration(500);
        animator.start();
    }


    /**
     * 关闭dialog 旋转floatactionbutton
     */
    public static void fabClose(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", -155, 20);
        animator.setDuration(500);
        animator.start();
    }

    /**
     * 打开酬劳界面
     */
    public static void llOpen(final Context context, final LinearLayout view) {
        int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, context.
                getResources().getDisplayMetrics());
        view.setVisibility(View.VISIBLE);
        ValueAnimator animator = ValueAnimator.ofInt(0, height);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
                params.height = value;
                view.setLayoutParams(params);
            }
        });
        animator.start();
    }

    /**
     * 关闭酬劳界面
     */
    public static void llClose(final Context context, final LinearLayout view) {
        int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, context.
                getResources().getDisplayMetrics());
        view.setVisibility(View.GONE);
        ValueAnimator animator = ValueAnimator.ofInt(height, 0);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
                params.height = value;
                view.setLayoutParams(params);
            }
        });
        animator.start();
    }

    /**
     * 打开编辑界面
     */
    public static void ryOpen(final Context context, final RecyclerView view) {
        final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -60, context.
                getResources().getDisplayMetrics());

        ValueAnimator animator = ValueAnimator.ofInt(margin, 0);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {

                int value = (int) animation.getAnimatedValue();
                LogUtil.d("value:" + value);
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
                params.setMargins(value, 0, 0, 0);
                view.setLayoutParams(params);
            }
        });
        animator.start();
    }

    /**
     * 关闭编辑界面
     */
    public static void ryClose(final Context context, final RecyclerView view) {
        final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -60, context.
                getResources().getDisplayMetrics());
        ValueAnimator animator = ValueAnimator.ofInt(0, margin);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
                params.setMargins(value, 0, 0, 0);
                view.setLayoutParams(params);
            }
        });
        animator.start();
    }
}
